package com.dblog.client.blog;

import com.dblog.client.gwt.RichTextEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PostForm extends FormPanel {

	public PostForm() {
		super();
		// Create a FormPanel and point it at a service.
	    this.setAction("/blog/new");

	    // Because we're going to add a FileUpload widget, we'll need to set the
	    // form to use the POST method, and multipart MIME encoding.
	    this.setEncoding(FormPanel.ENCODING_MULTIPART);
	    this.setMethod(FormPanel.METHOD_POST);

	    final TextBox title = new TextBox();
	    title.setName("title");
	    title.setWidth("100%");
	    
	    final RichTextEditor content = new RichTextEditor();
	    content.setName("content");
	    
	    final Hidden _content = new Hidden();
	    _content.setName("_content");
	    
	    final TextBox tags = new TextBox();
	    tags.setName("tags");
	    tags.setWidth("100%");
	    
	    // Create a panel to hold all of the form widgets.
	    VerticalPanel panel2 = new VerticalPanel();
	    panel2.setWidth("100%");
	    panel2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	    
	    VerticalPanel panel = new VerticalPanel();
	    panel.setWidth("75%");
	    panel2.add(panel);
	    
	    this.setWidget(panel2);

	    panel.add(new Label("Title"));
	    panel.add(title);
	    
	    panel.add(new Label("Content"));
	    panel.add(content);
	    
	    panel.add(new Label("Tags"));
	    panel.add(_content);
	    panel.add(tags);

	    // Add a 'submit' button.
	    panel.add(new Button("Submit", new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        PostForm.this.submit();
	      }
	    }));

	    // Add an event handler to the form.
	    this.addSubmitHandler(new FormPanel.SubmitHandler() {
	      public void onSubmit(SubmitEvent event) {
	        // This event is fired just before the form is submitted. We can take
	        // this opportunity to perform validation.
	        if (title.getText().length() == 0) {
	          Window.alert("The text box must not be empty");
	          event.cancel();
	        }
	      }
	    });
	    
	    this.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
	      public void onSubmitComplete(SubmitCompleteEvent event) {
	        Window.alert(event.getResults());
	      }
	    });
	    
	    setWidth("100%");
	}

}
